﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fcdbas.Core;
using Fcdbas.WinUI.Common;
namespace Fcdbas.Biz
{
    internal class SysCache
    {
        public static bool DownLoadFndUserProfile(string identify)
        {
            //return true;
            if (!IsRefresh("fnd_user_profile"))
                return true;
            string where = FndDAL.BuildCondition(new WhereValue("identity", identify, "=", true));
            DateTime dt = DateTime.Now;
            EntityList<fnd_user_profile> fndUP = fnd_user_profile.FindAll(where,"","",0,0);
            string conName = fnd_user_profile.Meta.ConnName;
            fnd_user_profile.Meta.ConnName = FndDAL.SQLite;
            fnd_user_profile.Meta.BeginTrans();
            fnd_user_profile.Meta.Execute("delete from fnd_user_profile_tab where " + where);
            fnd_user_profile.Meta.Commit();
            fndUP.Insert(false);
           
            fnd_user_profile.Meta.ConnName = conName;
            UpdateUserCacheFlag("fnd_user_profile", dt);
            return true;
        }
        public static bool DownLoadFndUserSecurity(string identify)
        {
            //return true;
            if (!IsRefresh("fnd_user_security"))
                return true;
            string where = FndDAL.BuildCondition(new WhereValue("identity", identify, "=", true));
            DateTime dt = DateTime.Now;
            EntityList<fnd_user_security> fndUP = fnd_user_security.FindAll(where,"","",0,0);
            string conName = fnd_object_security.Meta.ConnName;
            fnd_user_security.Meta.ConnName = FndDAL.SQLite;
            fnd_user_security.Meta.BeginTrans();
            fnd_user_security.Meta.Execute("delete from fnd_user_security_tab where " + where);
            fnd_user_security.Meta.Commit();
            fndUP.Insert(false);
            
            fnd_user_security.Meta.ConnName = conName;
            UserSecurity.UserSecs = fndUP;
            UpdateUserCacheFlag("fnd_user_security", dt);
            return true;
        }
        public static bool DownLoadFndEnumerate()
        {
            //return true;
            if (!IsRefresh("fnd_enumerate"))
                return true;
            cSessionManager.Enumerates = null;
            DateTime dt = DateTime.Now;
            EntityList<fnd_enumerate> fndUP = fnd_enumerate.FindAll("", "", "", 0, 0);
            string conName = fnd_enumerate.Meta.ConnName;
            fnd_enumerate.Meta.ConnName = FndDAL.SQLite;
            fnd_enumerate.Meta.BeginTrans();
            fnd_enumerate.Meta.Execute("delete from fnd_enumerate_tab ");
            fnd_enumerate.Meta.Commit();
            fndUP.Insert(false);

            fnd_enumerate.Meta.ConnName = conName;
           
            UpdateUserCacheFlag("fnd_enumerate", dt);
            return true;
        }
        public static bool DownLoadFndVpdPolicy(string identity)
        {
            //return true;
            if (!IsRefresh("fnd_vpd_policy"))
                return true;
            
            DateTime dt = DateTime.Now;
            string where = FndDAL.BuildCondition(new WhereValue("user_id", identity, "=", true));
            EntityList<fnd_vpd_policy> fndUP = fnd_vpd_policy.FindAll(where, "", "", 0, 0);
            string conName = fnd_vpd_policy.Meta.ConnName;
            fnd_vpd_policy.Meta.ConnName = FndDAL.SQLite;
            fnd_vpd_policy.Meta.BeginTrans();
            fnd_vpd_policy.Meta.Execute("delete from fnd_vpd_policy_tab where " + where);
            fnd_vpd_policy.Meta.Commit();
            fndUP.Insert(false);
           
            fnd_vpd_policy.Meta.ConnName = conName;
            Fcdbas.WinUI.Controls.cDataSource.VpdPolicys = fndUP;
            UpdateUserCacheFlag("fnd_vpd_policy", dt);
            return true;
        }
        public static bool IsRefresh(string preobject)
        {
            
            Dictionary<SalString, object> dic = new Dictionary<SalString, object>();
            dic.Add("cache_flag_", 0);
            dic.Add("table_name_", preobject);
            
            SalBoolean res = cSessionManager.DbMySQLBlock("FndSession_UserCache(:cache_flag_,:table_name_)", dic);
            if (res)
            {
                int iRes = 0;
                if (dic["cache_flag_"] != null)
                {
                    iRes = int.Parse(dic["cache_flag_"].ToString());
                    if (iRes != 0)
                        res = true;
                }
            }
            else
            {
                SalString sRes = "";
                cSessionManager.GetSqlLastError(ref sRes);
                WinTranslate.GetTranslate().ShowMessage(sRes, "", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
           
            }
            return res;
        }

        public static void UpdateUserCacheFlag(string preobject,DateTime dt)
        {

            Dictionary<SalString, object> dic = new Dictionary<SalString, object>();
            dic.Add("table_name_", preobject);
            dic.Add("cached_date_", dt);
            cSessionManager.DbTransactionBegin();
            SalBoolean res = cSessionManager.DbMySQLBlock("FndSession_UserCached(:table_name_,:cached_date_)", dic);
            if (res)
            {
                cSessionManager.DbTransactionEnd();
            }
            else
            {
                cSessionManager.DbTransactionClear();
                SalString sRes = "";
                cSessionManager.GetSqlLastError(ref sRes);
                WinTranslate.GetTranslate().ShowMessage(sRes, "", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
           
            }
           
        }
    }
}
